package com.ayf.payment.game.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ayf.payment.game.api.dto.PartitionDTO;
import com.ayf.payment.game.api.dto.criteria.MessageBoxCriteria;
import com.ayf.payment.game.api.dto.platform.NetConfigDTO;
import com.ayf.payment.game.api.entity.MessageBox;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MessageBoxMapper;
import com.ayf.payment.game.api.service.MessageBoxService;
import com.ayf.payment.game.api.service.PartitionService;
import com.ayf.payment.game.api.service.platform.NetConfigService;
import com.ayf.payment.game.api.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MessageBoxServiceImpl implements MessageBoxService {

    @Resource
    private MessageBoxMapper messageBoxMapper;
    @Autowired
    private PartitionService partitionService;
    @Autowired
    private NetConfigService netConfigService;

    @Override
    public boolean add(MessageBox messageBox) {
        if (messageBox == null) {
            throw new ParameterException("留言信息不能为空");
        }
        if (StringUtils.isBlank(messageBox.getGameAccount())) {
            throw new ParameterException("游戏账号不能为空");
        }
        if (StringUtils.isBlank(messageBox.getGameQq())) {
            throw new ParameterException("留言QQ不能为空");
        }
        if (StringUtils.isBlank(messageBox.getPartitionId())) {
            throw new ParameterException("分区id不能为空");
        }
        if (StringUtils.isBlank(messageBox.getPartitionName())) {
            throw new ParameterException("分区名称不能为空");
        }
        if (StringUtils.isBlank(messageBox.getContent())) {
            throw new ParameterException("留言内容不能为空");
        }
        NetConfigDTO netConfig = netConfigService.getNetConfig();
        String boxHideKey = netConfig.getBoxHideKey();
        if (StrUtil.isNotEmpty(boxHideKey)) {
            String[] split = boxHideKey.split("\\|");
            for (String key : split) {
                if (messageBox.getContent().toLowerCase().contains(key.toLowerCase())) {
                    throw new ParameterException("留言内容不能包含关键字：" + key);
                }
            }
        }
       PartitionDTO partitionDTO =  partitionService.getPartitionInfoById(messageBox.getPartitionId());
        if (partitionDTO == null){
            throw new ParameterException("分区信息不存在");
        }
        messageBox.setMerchantId(partitionDTO.getMerchantId());
        Date date = new Date();
        messageBox.setMessageTime(date);
        messageBox.setMessageTimeIndex(DateUtils.dateToInteger(date));
        return messageBoxMapper.insertSelective(messageBox) > 0;
    }

    @Override
    public boolean addWx(MessageBox messageBox) {
        if (messageBox == null) {
            throw new ParameterException("留言信息不能为空");
        }
        if (StringUtils.isBlank(messageBox.getGameAccount())) {
            throw new ParameterException("游戏账号不能为空");
        }
        if (StringUtils.isBlank(messageBox.getGameQq())) {
            throw new ParameterException("留言QQ不能为空");
        }
        if (StringUtils.isBlank(messageBox.getPartitionName())) {
            throw new ParameterException("分区名称不能为空");
        }
        if (StringUtils.isBlank(messageBox.getContent())) {
            throw new ParameterException("留言内容不能为空");
        }
        messageBox.setMessageTime(new Date());
        return messageBoxMapper.insertSelective(messageBox) > 0;
    }

    @Override
    public List<MessageBox> selectAllMsg(MessageBoxCriteria messageBoxCriteria) {
        if (StringUtils.isNotBlank(messageBoxCriteria.getStartTime())) {
            messageBoxCriteria.setStartTimeIndex(DateUtils.dateToInteger(DateUtils.stringToDate(messageBoxCriteria.getStartTime())));
        }
        if (StringUtils.isNotBlank(messageBoxCriteria.getEndTime())) {
            messageBoxCriteria.setEndTimeIndex(DateUtils.dateToInteger(DateUtils.stringToDate(messageBoxCriteria.getEndTime())));
        }
        return messageBoxMapper.selectAllMsg(messageBoxCriteria);
    }

    @Override
    public boolean updateShow(MessageBoxCriteria messageBoxCriteria) {
        if (messageBoxCriteria == null) {
            throw new ParameterException("条件不能为空");
        }
        if (messageBoxCriteria.getIds() == null || messageBoxCriteria.getIds().size() == 0) {
            throw new ParameterException("留言箱ID不能为空");
        }
        if (messageBoxCriteria.getMerchantId() == null) {
            throw new ParameterException("商户ID不能为空");
        }
        return messageBoxMapper.updateShow(messageBoxCriteria) > 0;
    }

    @Override
    public int selectMessageNum(MessageBoxCriteria messageBoxCriteria) {
        if (messageBoxCriteria == null) {
            throw new ParameterException("条件不能为空");
        }
        if (messageBoxCriteria.getMerchantId() == null) {
            throw new ParameterException("商户ID不能为空");
        }
        return messageBoxMapper.selectMessageNum(messageBoxCriteria);
    }

    @Override
    public boolean delMeg(MessageBoxCriteria messageBoxCriteria) {
        return messageBoxMapper.delMeg(messageBoxCriteria) > 0;
    }
}




